
//////////////////////////////////////////////////////////////////////////////////
//	Drawing.cs
//
//	Written by Cosic Nadin and Franz Thomas Reinisch FH-Joanneum ITM06
//  
//////////////////////////////////////////////////////////////////////////////////



using System;
using System.Collections.Generic;
using System.Text;
using WiimoteLib;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Configuration;

namespace WiiFlipChart
{
    class Drawing
    {

        private Point lastPoint;
        private Point newPoint;
        private Graphics currentGrfx;
        private Pen myPen;
        
        
        static readonly Drawing instance = new Drawing();

        public static Drawing Instance
        {
            get
            {
                return instance;
            }
        }



        /// <summary>
        /// Construcor
        /// Load in the CurrentGraphics The Bmp(currentPage)
        /// Subscribe Eventhandler Button A Pressed on WiiMOTE
        /// Subscribe Eventhandler CurrentDrawPointChanged from WiiMote
        /// Subscribe Eventhandler CurrentPageChanged  to load the new Picture in the Grafix
        /// </summary>
        public  Drawing()
        {
            
            myPen = new Pen(Color.Black);
            
            //Load in the CurrentGraphics The Bmp(currentPage)
            currentGrfx = Graphics.FromImage(PageControl.Instance.getCurrentPage());

            //Subscribe Eventhandler Button A Pressed on WiiMOTE
            Controller.Instance.ButtonAChanged += new EventHandler(DrawFree);

            //Subscribe Eventhandler CurrentDrawPointChanged from WiiMote
            Controller.Instance.CurrentDrawCursorPointChanged += new EventHandler(CurrentDrawCursorPointChanged);


            //Subscribe Eventhandler CurrentPageChanged  to load the new Picture in the Grafix
            PageControl.Instance.CurrentPageChanged += new EventHandler(CurrentPageChanged);


        }

        //Eventhandlerbased Methods



        /// <summary>
        /// Draw if ther was presssed the button "b"
        /// </summary>
        /// <param name="objSender"></param>
        /// <param name="e"></param>
        protected void DrawFree(Object objSender, EventArgs e)
        {
                     
            //Load the current color for  pen
            myPen.Color = ColorControl.Instance.getcurrentColor();

            //Load the current StrokeWidth for pen
            myPen.Width = StrokeWidthControl.Instance.getcurrentstrokewidth();


            //get next Point
            newPoint = new Point(Controller.Instance.getCurrentDrawCursorPoint().X - Controller.Instance.getDrawFieldPositionX() ,
                                    Controller.Instance.getCurrentDrawCursorPoint().Y - Controller.Instance.getDrawFieldPositionY()); 

            currentGrfx.DrawLine(myPen, lastPoint, newPoint);

            lastPoint = newPoint;
            
        }


        /// <summary>
        /// If there was changed the current draw cursor
        /// </summary>
        /// <param name="objSender"></param>
        /// <param name="e"></param>
        protected void CurrentDrawCursorPointChanged(Object objSender, EventArgs e)
        {
            this.lastPoint.X = Controller.Instance.getCurrentDrawCursorPoint().X - Controller.Instance.getDrawFieldPositionX();
            this.lastPoint.Y = Controller.Instance.getCurrentDrawCursorPoint().Y - Controller.Instance.getDrawFieldPositionY();
        }


        protected void CurrentPageChanged(Object objSender, EventArgs e)
        {
            //Load in the CurrentGraphics the currentPage
            currentGrfx = Graphics.FromImage(PageControl.Instance.getCurrentPage());
        }









    }
}
